package com.total.reporting.presentation;
import com.total.reporting.dao.*;
import java.util.*;
import java.io.IOException;
import java.io.OutputStream;
import java.net.URLEncoder;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.json.JSONObject;

/**
 * Servlet implementation class reportingServlet
 */
public class reportingServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;
	private Map<String,String> treeOpenedIconsMap=new HashMap();
	private Map<String,String> treeClosedIconsMap=new HashMap();
	
       
    /**
     * @see HttpServlet#HttpServlet()
     */
    public reportingServlet() {
        super();
        // TODO Auto-generated constructor stub
    }
    public void init(){
    	treeClosedIconsMap=ConceptController.getConceptClosedIcons();
    	treeOpenedIconsMap=ConceptController.getConceptOpenedIcons();
    	this.getServletContext().setAttribute("treeClosedIconsMap", treeClosedIconsMap);
    	this.getServletContext().setAttribute("treeOpenedIconsMap", treeOpenedIconsMap);
    }

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		processRequest(request,response);
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		processRequest(request,response);
	}
	
	protected void processRequest(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		String action=request.getParameter("action");
		if(action==null || action.length()==0)
			return;
		String id=request.getParameter("id");
		
		if(action.equals("getItemsByType")){
			this.replySerializeJSON(ItemController.getItemsByType(id),response);
		}
		else if(action.equals("getItemsByParentId")){		
				this.replySerializeJSON(ItemController.getItemsByParentId(id),response);
		}
		else if(action.equals("addItem")){	
			Item item=new Item();
			item.setName(id);
			item.setParentId(Integer.parseInt(request.getParameter("parentId")));
			item.setType(request.getParameter("type"));
			item.setDateCreation(new Date());
			item.setDateModification(new Date());
			item.setCreatedBy("xxxx");
			item.setModifiedBy("xxxx");
			int newId=ItemController.saveItem(item);
			this.replySerializeJSON(ItemController.getItemsById(""+newId),response);
	}	
		else if(action.equals("RSS")){
			OutputStream os= response.getOutputStream();
			ReportController.generateRSSweeklyReport(request,os);
			os.flush();
			os.close();
			return;
		}
		
			
	
	}
	
	private  void replySerializeJSON(List<Item> list,HttpServletResponse response ) {
		try{
			Iterator<Item> it=list.listIterator();
			List<ItemSummary> list2= new ArrayList<ItemSummary>();
			while(it.hasNext()){
				Item item=it.next();
				ItemSummary item2=new ItemSummary();
				item2.setId(item.getId());
				item2.setName(URLEncoder.encode( item.getName(),"UTF-8").replace("+", " "));
				item2.setParentId(item.getParentId());
				item2.setType(URLEncoder.encode( item.getType(),"UTF-8"));
				list2.add(item2);
				item2.setIconClosed("images/"+treeClosedIconsMap.get(item.getType()));
				item2.setIconOpened("images/"+treeOpenedIconsMap.get(item.getType()));
			}			
			JSONObject obj = new JSONObject(new ItemSummaries(list2));	
			String str=obj.toString(1);
			response.getWriter().write(str);
		}
		catch(Exception e){
			try{
				response.getWriter().write(e.toString());
			}
			catch(Exception e2){
				e2.printStackTrace();
			}
			
		}
		
	}

}
